/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *   g.latarowski@gmail.com                                                *
 *   maciej.gajewski0@gmail.com                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


// gcommon
#include "gexception.h"

// local
#include "gparams.h"

const char * BLOCK_TAG  = "params";
const char * SINGLE_TAG = "param";
const char * NAME_TAG   = "name";
const char * TYPE_TAG   = "type";
const char * VALUE_TAG  = "value";


// ===========================================================================
//                                 GParams
// ===========================================================================


// -------------------- GParams::toXml() -------------------------------------
QDomElement GParams::toXml( QDomDocument & doc )const
{
	QDomElement params = doc.createElement( BLOCK_TAG );
	
	// create DOM tree from all params
	
	QMap<QString, QVariant>::const_iterator i;
	for( i = this->begin(); i != this->end(); i++ )
	{
			// create param node 
		QDomElement param = doc.createElement( SINGLE_TAG );
		params.appendChild( param );
			// append params name
		QDomElement name = doc.createElement( NAME_TAG );
		param.appendChild( name );
		QDomText nameText = doc.createTextNode( i.key() );
		name.appendChild( nameText );
		
			// append param 
		param.appendChild( variantToXml( doc, i.value() ) );
	}
	
	return params;
}
// ---------------------------------------------------------------------------


// ------------------------- GParams::fromXml() ------------------------------
void GParams::fromXml( const QDomElement & domElement )
{
	QDomElement paramsElement;
		// check if searched node isn't root 
	if( domElement.tagName() == BLOCK_TAG )
	{
		paramsElement = domElement;
	}
	else
	{
		paramsElement = domElement.firstChildElement( BLOCK_TAG );
	}
		// if there is no params then give up
	if( paramsElement.isNull() )
	{
		return;
	}

	// ====================================================
	// parse first param
	
	QDomElement paramElement = paramsElement.firstChildElement( SINGLE_TAG );
		// return if there is no params 
	if( paramElement.isNull() )
	{
		return;
	}
	// ------------------------------------------------
	// parse elements

		// parse name
	QDomElement nameElement = paramElement.firstChildElement( NAME_TAG );
		// parse value
	QVariant value = variantFromXml( paramElement );

	// ------------------------------------------------
	// insert "param" into parmas map - _params
	if( !nameElement.isNull() )
	{
			// insert
		this->insert( nameElement.text(), value );
	}

	// ====================================================
	// parse next params 
	while( !paramElement.nextSiblingElement().isNull() )
	{
			// get next element 
		paramElement = paramElement.nextSiblingElement();
		
			// parse name 
		QDomElement nameElement = paramElement.firstChildElement( NAME_TAG );
			// skip rest if there is no "name" in this "param" 
		if( nameElement.isNull() ) continue;
		
			// parse value 
		QVariant value = variantFromXml( paramElement );
		
		// ------------------------------------------------
		// insert "param" into parmas map - _params
		
			// insert 
		this->insert( nameElement.text(), value );
	}
}
// ---------------------------------------------------------------------------




// eof
